#include "Logger.h"

#include "UI/ServerWindow.h"

using namespace System;


void SPlayerServer::Logger::init(LogLevel level)
{
    currentLevel = level;
    serverLogger = gcnew Action<String ^>(SPlayerServer::ServerWindow::window,
                                                  &SPlayerServer::ServerWindow::log);
}

void SPlayerServer::Logger::log(LogLevel level, System::String^ format, ... array<System::Object^>^ args)
{
    if (level >= currentLevel)
    {
        auto now = DateTime::Now;
        writeLogText(String::Format("{0:00}-{1:00} {2:00}:{3:00}:{4:00}.{5:000} : {6}\n\n",
            now.Month, now.Day, now.Hour, now.Minute, now.Second, now.Millisecond,
            String::Format(format, args)));
    }
}

void SPlayerServer::Logger::writeLogText(String ^text)
{
    if (SPlayerServer::ServerWindow::window->InvokeRequired)
    {
        SPlayerServer::ServerWindow::window->Invoke(serverLogger, text);
    }
    else
    {
        serverLogger(text);
    }
}
